import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/network/http_config.dart';
import 'package:sui_shou_pai/network/http_request.dart';
import 'package:sui_shou_pai/record/record_card_list.dart';
import 'package:sui_shou_pai/record/record_entity/photo_record_entity.dart';

class RecordCardListType2 extends StatefulWidget {
  const RecordCardListType2({Key key}) : super(key: key);

  @override
  _RecordCardListType2State createState() => _RecordCardListType2State();
}

List<PhotoRecordEntity> photoRecordEntityList = [];
List<PhotoRecordEntity> plist = [];

class _RecordCardListType2State extends State<RecordCardListType2> {
  // 请求数据
  Future<void> getData() async {
    plist.clear();
    HttpRequest.request("$BASE_URL/photo-record/getPhotoRecordListType2",
            method: "GET", token: await StorageUtil.getStringItem("token"))
        .then((value) {
      print('数据：$value');
      var data = jsonDecode(value.toString());
      // print('解码后数据：$data');
      List<dynamic> res = data['result'];
      // print("res数据："+res.toString());
      for (int i = 0; i < res.length; i++) {
        print("res第" + i.toString() + "个值" + res[i].toString());
        PhotoRecordEntity photoRecordEntity =
            PhotoRecordEntity.fromJson(res[i]);
        plist.add(photoRecordEntity);
      }
      print("plist.length" + plist.length.toString());
      setState(() {
        // getData();
        print(plist.length);
        photoRecordEntityList.clear();
        if (plist.isNotEmpty) {
          for (int i = 0; i < plist.length; i++) {
            photoRecordEntityList.add(plist[i]);
          }
          print(photoRecordEntityList[0].title);
        }
      });
    });
  }

  // 图片字符串拼接
  String imageSplit(String image) {
    List<String> imageList = image.split(',');
    if (imageList.isNotEmpty) {
      return imageList[0];
    } else
      return "https://raw.githubusercontent.com/bestpure/assets/master/20210723152419.png";
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
    // 新建监听
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
        onRefresh: onRefresh,
        child: RecordCardList(photoRecordEntityList: photoRecordEntityList));
  }

  // 下拉刷新
  Future<void> onRefresh() async {
    setState(() {
      getData();
    });
    Fluttertoast.showToast(
      msg: "刷新成功",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.BOTTOM,
    );
  }
}
